package org.zxp.thread.tools;

import java.util.concurrent.CountDownLatch;

/**
 * @program: zxp-thread-test
 * @description: CountDownLatch测试
 * @author: X-Pacific zhang
 * @create: 2019-05-24 20:58
 **/
public class CountDownLatchTest {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(3);
        for (int i = 0; i < 3; i++) {
            int j = i;
            new Thread(() -> {
                try {
                    Thread.sleep(1000*j);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("子线程执行完毕");
                countDownLatch.countDown();
            }).start();
        }
        new Thread(() -> {
            try {
                countDownLatch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("S子线程执行完毕");
        }).start();
        countDownLatch.await();
        System.out.println("主线程继续~");
    }
}
